package com.partsoft.dits;

import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

@SuppressWarnings("unchecked")
public abstract class AbstractDescriptorResolver implements DescriptorResolver {

	public boolean hasDescriptor(String dclsName, String id) {
		if (!StringUtils.hasText(dclsName) || !StringUtils.hasText(id)) return false;
		Class<?> dcls = null;
		try {
			dcls = ClassUtils.forName(dclsName, Thread.currentThread().getContextClassLoader());
		} catch (Exception e) {
			throw new IllegalArgumentException(new StringBuilder("(").append(dclsName).append(")不是有效的类名").toString(), e);
		}
		Assert.isAssignable(EdgeDescriptor.class, dcls, new StringBuilder("(").append(dclsName).append(")不是有效描述类实现").toString());
		return hasDescriptor((Class<? extends EdgeDescriptor>)dcls, id);
	}


	public <E extends EdgeDescriptor> E getDescriptor(String dclsName, String id) {
		Class<?> dcls = null;
		try {
			dcls = ClassUtils.forName(dclsName, Thread.currentThread().getContextClassLoader());
		} catch (Exception e) {
			throw new IllegalArgumentException(new StringBuilder("(").append(dclsName).append(")不是有效的类名").toString(), e);
		}
		Assert.isAssignable(EdgeDescriptor.class, dcls, new StringBuilder("(").append(dclsName).append(")不是有效描述类实现").toString());
		return getDescriptor((Class<? extends EdgeDescriptor>)dcls, id);
	}
}
